package com.demo.test;

import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import com.demo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserTest1 {
    @Test
    public void testFindById(){
        //创建SqlSession实例
        SqlSession session = MyBatisUtils.getSession();
        //获取Mapper
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //调用Mapper的方法
        User user = userMapper.findById(2);
        System.err.println(user);
        //关闭session
        session.close();
    }

    @Test
    public void testFindAll() { //通过Mapper接口查询全部用户
        //创建SqlSession实例
        SqlSession session = MyBatisUtils.getSession();
        //获取Mapper
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //调用Mapper的方法
        List<User> users = userMapper.findAll();
        //输出结果
        for (User user : users)
            System.err.println(user);
        //关闭session
        session.close();
    }
}
